package com.csv2excel;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class Main implements Printer {
    //创建窗体，JFrame
    JFrame f = new JFrame("CSV转Excel");
    String sourceFile;
    String sourceFileName;
    String targetFileName;
    String targetPath;
    JPanel northPanel = new JPanel();
    JTextPane sourceFileTextPane = new JTextPane();
    JTextPane targetPathTextPane = new JTextPane();

    //转换提示
    JTextArea textArea = new JTextArea(10,20);
    JScrollPane scrollPane = new JScrollPane(textArea);

    public void init(){
        addOperationComponent();
        addTipComponent();
        //设置关闭窗口时，退出程序
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setPreferredSize(new Dimension(800, 600));
//        f.setLocationRelativeTo(null);
        f.setVisible(true);
        f.pack();
    }

    /** 操作区域 */
    private void addOperationComponent() {
        northPanel.setLayout(new GridLayout(3,2));
        //添加源文件选择组件
        addSourceButton();
        //添加目标选择组件
        addTargetButton();
        //添加转换按钮
        addTransformButton();
        //添加转换按钮
        f.add(northPanel, BorderLayout.NORTH);
    }
    /** 提示内容框 */
    private void addTipComponent() {
        textArea.setEnabled(false);
        scrollPane.setAutoscrolls(true);
        f.add(scrollPane, BorderLayout.CENTER);
    }


    public void addSourceButton(){
        JButton source = new JButton("请选择csv源文件");
        source.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc=new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                jfc.setAcceptAllFileFilterUsed(false);
                jfc.setFileFilter(new FileNameExtensionFilter("请选择csv文件", "csv"));
                int result = jfc.showOpenDialog(null);
                if (JFileChooser.APPROVE_OPTION==result) {
                    File file = jfc.getSelectedFile();
                    if (file == null) {
                        return;
                    }
                    sourceFile = file.getAbsolutePath();
                    sourceFileName = file.getName();
                    targetFileName = sourceFileName.replace(".csv", ".xlsx");
                    sourceFileTextPane.setText(sourceFile);

                    if (targetPath != null && !targetPath.isEmpty()) {
                        targetPathTextPane.setText(targetPath + File.separator + targetFileName);
                    }
                }
            }
        });
        northPanel.add(source);

        sourceFileTextPane.setEnabled(false);
        northPanel.add(sourceFileTextPane);
    }

    public void addTargetButton(){
        JButton target = new JButton("请选择目标路径");
        target.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc=new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int result = jfc.showOpenDialog(null);
                if (JFileChooser.APPROVE_OPTION==result) {
                    File file = jfc.getSelectedFile();
                    if (file == null) {
                        return;
                    }
                    targetPath = file.getAbsolutePath();
                    if (sourceFile != null && !sourceFile.isEmpty()) {
                        targetPathTextPane.setText(targetPath + File.separator + targetFileName);
                    } else {
                        targetPathTextPane.setText(targetPath);
                    }
                }
            }
        });
        northPanel.add(target);

        targetPathTextPane.setEnabled(false);
        northPanel.add(targetPathTextPane);
    }

    public void csv2excel(){
        if (sourceFile==null || sourceFile.isEmpty()){
            textArea.append("请选择源文件\n");
            return;
        }
        if (targetPath==null || targetPath.isEmpty()){
            textArea.append("请选择目标文件路径\n");
            return;
        }

        new Thread(new Csv2Excel(this, sourceFile, targetPath, targetFileName)).start();
    }

    public void addTransformButton(){
        JButton transform = new JButton("转换");
        transform.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                csv2excel();
            }
        });
        northPanel.add(transform);
    }

    public void println(String message){
        textArea.append(message+"\n");
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    public static void main(String[] args){
        new Main().init();
    }
}
